package com.ynu.springbootappdemo.controller;

import com.ynu.springbootappdemo.common.controller.LogicController;
import com.ynu.springbootappdemo.common.exception.BusinessException;
import com.ynu.springbootappdemo.common.utils.BaseResponse;
import com.ynu.springbootappdemo.common.utils.ErrorCode;
import com.ynu.springbootappdemo.common.utils.ResultUtils;
import com.ynu.springbootappdemo.dao.IFoodDao;
import com.ynu.springbootappdemo.model.dto.food.FoodAddRequest;
import com.ynu.springbootappdemo.model.dto.food.FoodQueryRequest;
import com.ynu.springbootappdemo.model.entity.Food;
import com.ynu.springbootappdemo.model.vo.FoodVo;
import com.ynu.springbootappdemo.service.FoodService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Api(tags = "美食控制器")
@RestController
@RequestMapping("/food")
public class FoodController extends LogicController<FoodService, IFoodDao, Food, Long> {
    public FoodController(@Autowired FoodService service) {
        super(service);
    }


    @PostMapping("/getFoodsByCityId")
    @ApiOperation("获取城市的食物列表")
    public BaseResponse<List<FoodVo>> getFoodsByCityId(@ApiParam("查询条件") @RequestBody FoodQueryRequest foodQueryRequest) {
        System.out.println("foodQueryRequest = " + foodQueryRequest.getFoodId());
        return ResultUtils.success(service.getFoodsByCityId(foodQueryRequest.getCityId()));
    }

    @PostMapping("/getFoodsByCityIdExpectFoodId")
    @ApiOperation("获取城市的其他食物列表")
    public BaseResponse<List<FoodVo>> getFoodsByCityIdExpectFoodId(@ApiParam("查询条件") @RequestBody FoodQueryRequest foodQueryRequest) {
        return ResultUtils.success(service.getFoodsByCityIdExceptFoodId(foodQueryRequest.getCityId(), foodQueryRequest.getFoodId()));
    }
    /**
     * 添加食物(内部调用)
     * @Param CityAddRequest
     * @Return BaseResponse<CityVo>
     */
    @PostMapping("/inner/add")
    @ApiOperation("添加食物(内部调用)")
    public BaseResponse<FoodVo> addInner(@ApiParam("美食添加对象") FoodAddRequest foodAddRequest) {
        FoodVo foodVo = service.add(foodAddRequest);
        if (Objects.isNull(foodVo)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return ResultUtils.success(foodVo);
    }
}
